// @flow
import React, { Component } from 'react'
import { AgGridReact } from 'ag-grid-react'
import { TableResize } from 'ag-grid-addons'
import styles from './MenuSettingGrid.css'
import MenuSettingActions from './MenuSettingActions'
import Pagination from 'components/Pagination'

type Props = {
  rowData: Array<Object>,
  refreshMenuSettings: Function,
  history: Object,
  total: number,
  current: number,
  pageSize: number
}
type States = {
  columnDefs: Array<Object>
}
class MenuSettingGrid extends Component {
  props: Props
  state: States
  gridOptions: Object
  onGridReady: Function
  api: Object
  constructor (props: Props) {
    super(props)
    this.state = {
      columnDefs: [
        {
          headerName: '名称',
          field: 'name',
          cellStyle: {
            'padding': '8px'
          },
          cellRenderer: 'group',
          cellRendererParams: {
            // checkbox: true,
            innerRenderer:  (params) => {
              return params.data.name
            }
          }
        },
        {
          headerName: '链接',
          field: 'href',
          cellStyle: {
            padding: '8px'
          }
        },
        {
          headerName: '排序',
          field: 'sort',
          cellStyle: {
            padding: '8px'
          }
        },
        {
          headerName: '可见',
          field: 'isShow',
          cellStyle: {
            padding: '8px'
          },
          cellRenderer: (params) => {
            if (params.data.isShow === 1) {
              return '显示'
            } else {
              return '隐藏'
            }
          }
        },
        {
          headerName: '权限标识',
          field: 'permission',
          cellStyle: {
            padding: '8px'
          }
        },
        {
          headerName: '操作',
          cellRendererParams: {
            history: this.props.history,
            current: this.props.current,
            pageSize: this.props.pageSize,
            refreshMenuSettings: this.props.refreshMenuSettings
          },
          cellRendererFramework: MenuSettingActions,
          cellStyle: {
            padding: '8px',
            'text-align': 'center'
          }
        }
      ]
    }
    this.gridOptions = {
      localeText: {
        loadingOoo: '数据加载中...',
        noRowsToShow: '暂无数据',
        filterOoo: '搜索当前页面',
        equals: '等于',
        notEqual: '不等于',
        contains: '包含',
        startsWith: '开始于',
        endsWith: '结束于',
        copy: '复制',
        paste: '粘贴',
        copyWithHeaders: '带表头复制',
        toolPanel: '工具栏',
        export: '导出',
        csvExport: '导出CSV',
        excelExport: '导出Excel'
      },
      enableSorting: true,
      enableFilter: true,
      pagination: false,
      // animateRows: true,
      rowSelection: 'multiple',
      enableColResize: true,
      enableSorting: true,
      // groupSelectsChildren: true,
      // suppressCopyRowsToClipboard: true,
      icons: {
        groupExpanded: '<i class="fa fa-minus-square-o" aria-hidden="true"></i>',
        groupContracted: '<i class="fa fa-plus-square-o" aria-hidden="true"></i>'
      },
      onModelUpdated: () => {
        if (this.api) {
          this.api.sizeColumnsToFit()
          this.TableResize()
        }
      },
      getNodeChildDetails: (node: Object) => {
        // console.log(node)
        if (node.children.length > 0) {
          return {
            group: true,
            children: node.children
          }
        } else {
          return null
        }
      }
    }
    this.onGridReady = this.onGridReady.bind(this)
    this.TableResize = TableResize.bind(this)
  }

  onGridReady (params: Object) {
    this.api = params.api
    this.api.sizeColumnsToFit()
  }
  render () {
    const { columnDefs } = this.state
    const { rowData, current, pageSize, total, refreshMenuSettings } = this.props
    return (
      <div className={styles['grid'] + ' ag-fresh'}>
        <AgGridReact {...{
          gridOptions: this.gridOptions,
          rowHeight: '34',
          headerHeight: '28',
          columnDefs: columnDefs,
          rowData: rowData,
          onGridReady: this.onGridReady
        }}
          />
        <Pagination current={current} pageSize={pageSize} total={total} onChange={refreshMenuSettings} />
      </div>
    )
  }
}

export default MenuSettingGrid
